﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using Point = System.Drawing.Point;

namespace AutoLoL.Logic.Automation
{
    public class Win32
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //[DllImport("user32.dll")]
        //public static extern bool ScreenToClient(IntPtr hWnd, ref System.Drawing.Point lpPoint);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ClientToScreen(IntPtr hwnd, ref System.Drawing.Point lpPoint);

        [DllImport("user32.dll")]
        public static extern bool BlockInput(bool fBlockIt);

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImportAttribute("User32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref Win32Rect lpRect);

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

        [DllImport("gdi32.dll")]
        public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

        //[DllImport("gdi32.dll")]
        //public static extern bool DeleteDC(IntPtr hdc);

        //[DllImport("user32.dll")]
        //public static extern IntPtr GetWindowDC(IntPtr hWnd);

        //[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
        //public static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart, string lpString, int cbString);

        //[DllImport("user32.dll")]
        //public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

        //[DllImport("user32.dll")]
        //static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetCursorPos(out System.Drawing.Point lpPoint);

        [DllImport("user32.dll")]
        public static extern short GetKeyState(VirtualKeyStates nVirtKey);

        public enum VirtualKeyStates : int
        {
            VK_CAPITAL = 0x14
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct Win32Rect
        {
            public Int32 Left;
            public Int32 Top;
            public Int32 Right;
            public Int32 Bottom;
        };

        public static void GetWindowRectangle(IntPtr hWnd, ref Rect windowRect)
        {
            Win32Rect win32Rect = new Win32Rect();
            GetWindowRect(hWnd, ref win32Rect);

            windowRect = new Rect(win32Rect.Left, win32Rect.Top, win32Rect.Right - win32Rect.Left, win32Rect.Bottom - win32Rect.Top);
        }

        //public static void MouseClick()
        //{
        //    int x = System.Windows.Forms.Cursor.Position.X;
        //    int y = System.Windows.Forms.Cursor.Position.Y;

        //    if (!SystemInformation.MouseButtonsSwapped)
        //    {
        //        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        //    }
        //    else
        //    {
        //        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
        //    }
        //}

        public static Color GetPixelColor(IntPtr hWnd, int x, int y)
        {
            IntPtr hdc = GetDC(hWnd);
            uint pixel = GetPixel(hdc, x, y);
            ReleaseDC(hWnd, hdc);
            return Color.FromArgb((int)(pixel & 0x000000FF),
                         (int)(pixel & 0x0000FF00) >> 8,
                         (int)(pixel & 0x00FF0000) >> 16);
        }

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
    }
}
